﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using Sustainalytics.ClientDW.Entities.Universes;

namespace Sustainalytics.Synchronization.SyncFeatureUniverses
{
    public class UniverseCompanyHandle
    {
        private IMongoCollection<UniverseCompany> UniverseCompanyCollection { get; set; }
        public UniverseCompanyHandle(string connectionString)
        {
            var mongoClient = new MongoClient(connectionString);
            var database = mongoClient.GetDatabase(connectionString.Split('/').Last().Trim());
            UniverseCompanyCollection = database.GetCollection<UniverseCompany>(typeof (UniverseCompany).Name);
        }
        public Dictionary<Guid, List<int>> GetAllUniverseCompany()
        {
            return UniverseCompanyCollection
                .Aggregate()
                .Project(x => new {x.CompanyId, x.UniverseId})
                .Group(x => x.CompanyId,
                    g => new {CompanyId = g.Key, UniverseIds = g.Select(x => x.UniverseId).ToList()})
                .ToList()
                .ToDictionary(x => x.CompanyId, x => x.UniverseIds);
        }
    }
}
